package com.example.service;

import com.example.controller.WebSocketController;
import com.example.mapper.ChatMapper;
import com.example.mapper.JobRecruiterMapper;
import com.example.mapper.JobSeekerMapper;
import com.example.pojo.dao.ChatRecordsRepository;
import com.example.pojo.entity.Chat;
import com.example.pojo.entity.ChatRecords;
import com.example.pojo.entity.JobRecruiter;
import com.example.pojo.entity.JobSeeker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 所学内容:
 *
 * @author ..
 * @Date 2023/10/28 14:19
 */
@Service
public class ChatServiceImpl implements ChatService{

    private static ChatRecordsRepository chatRecordsRepository;

    @Autowired
    private void cr(ChatRecordsRepository repository){
        ChatServiceImpl.chatRecordsRepository=repository;
    }

    @Autowired
    private ChatMapper chatMapper;

    @Autowired
    private JobRecruiterMapper jobRecruiterMapper;

    @Autowired
    private JobSeekerMapper jobSeekerMapper;

    @Override
    public Chat findByJsJrID(Integer jsId,Integer jrId) {
        return chatMapper.findByJsJrID(jsId,jrId);
    }

    @Override
    public int insertJsJr(Integer jrId, Integer jsId) {
        JobRecruiter jobRecruiter = jobRecruiterMapper.fingJobRecruiterById(jrId);
        JobSeeker jobSeeker = jobSeekerMapper.findJSById(jsId);

        return chatMapper.insertJsJr(jrId,jobRecruiter.getCompanyName(),jobRecruiter.getJrAvatar(),jsId,jobSeeker.getJsName(),jobSeeker.getAvatar());
    }

    @Override
    public int updateLastTime(Date lastTime, Integer id) {
        return chatMapper.updateLastTime(lastTime,id);
    }

    @Override
    public void addCR(Integer chatId, Integer sige, String message) {
        ChatRecords chatRecords = new ChatRecords();
        chatRecords.setChatId(chatId);
        chatRecords.setSign(sige);
        chatRecords.setMessage(message);
        chatRecords.setSendTime(new Date());
        chatRecordsRepository.save(chatRecords);
        updateLastTime(chatRecords.getSendTime(),chatId);
    }

    @Override
    public List<Chat> findByJsId(Integer jsId) {
        return chatMapper.findByJsId(jsId);
    }

    @Override
    public List<Chat> findByJrId(Integer jrId) {
        return chatMapper.findByJrId(jrId);
    }


}
